<?php
	/**
	 * PkGateway File
	 *
	 * @author Brian Bal
	 */
	
	
	/**
	 * PkGateway class
	 *
	 * @package pink-koala
	 * @author Brian Bal
	 */
	
	class PkGateway
	{
		/**
		 * model_class
		 *
		 * this variable holds the class name that this instance of
		 * the PkGateway is acting as a Table Data Gateway for.
		 *
		 * @var	string
		 */
		public $model_class;
		
		
		
		/**
		 * PkGateway Class Constructor
		 * this function get a new instance of PkGateway ready
		 *
		 * @package default
		 * @author Brian Bal
		 */
		
		public function __construct($model_class)
		{
			$this->model_class = $model_class;
		}
		
		
		
		/**
		 * Runs a select on the orders table and returns the results as an array of orders
		 * It also loads the order_items by calling loadOrderItems()
		 * @param string $condition
		 * @param string $order
		 * @param string $limit
		 * @param string $offset
		 * @return array|User
		 */
		
		public function find($condition = "all", $order = "none", $limit = "none", $offset = "none")
		{
			$model = new $this->model_class();
			
			// init the found array
			$found = array();
			
			// build query parts
			$where = "";
			if($condition != "all") { $where = "WHERE $condition"; }
			
			$offset_limit = "";
			if($limit != "none") { $offset_limit = "LIMIT $limit"; }
			if($offset != "none") { $offset_limit = "$offset_limit OFFSET $offset"; }
			
			$order_by = "";
			if($order != "none") { $order_by = "ORDER BY $order"; }
			
			$columns = "";
			$count = 0;
			
			$table_columns = $model->getTableColumns();
			foreach($table_columns as $table_column)
			{
				$comma = ", ";
				if($count == 0){ $comma = ""; }
				
				$columns .= $comma . "`" . $table_column['Field'] . "`";
				
				$count ++;
			}
			
			// build the query
			$sql = "SELECT
				$columns
			FROM 
				`" . $model->getTableName() . "`
			$where
			$order_by
			$offset_limit
			";
			
			// echo "<pre>\n$sql\n</pre>";
			$class_name = $this->model_class;
			
			// run the query and get results
			$result = mysql_query($sql);
			while($row = mysql_fetch_array($result))
			{
				$tmp = new $class_name();
				foreach($row as $key => $val)
				{
					$tmp->$key = $val;
				}
				array_push($found, $tmp);
			}
			
			return $found;
		}
		
		
		
		/**
		 * findIDs function
		 * 
		 * findIds takes the parameters for a query then returns an array
		 * of all the matching models primary keys
		 *
		 * @param string $condition 
		 * @param string $order 
		 * @param string $limit 
		 * @param string $offset 
		 * @return array
		 * @author Brian Bal
		 */
		
		function findIDs($condition = "all", $order = "none", $limit = "none", $offset = "none")
		{
			$model = new $this->model_class();
			
			// init the found array
			$found = array();
			
			// build query parts
			$where = "";
			if($condition != "all") { $where = "WHERE $condition"; }
			
			$offset_limit = "";
			if($limit != "none") { $offset_limit = "LIMIT $limit"; }
			if($offset != "none") { $offset_limit = "$offset_limit OFFSET $offset"; }
			
			$order_by = "";
			if($order != "none") { $order_by = "ORDER BY $order"; }
			
			
			// build the query
			$sql = "SELECT
				" . $model->getPrimaryKey() . " as `id`
			FROM 
				`" . $model->getTableName() . "`
			$where
			$order_by
			$offset_limit
			";
			
			// echo "<pre>\n$sql\n</pre>";
			
			// run the query and get results
			$result = mysql_query($sql);
			while($row = mysql_fetch_array($result))
			{
				array_push($found, $row['id']);
			}
			
			return $found;
		}
		
		
		
		/**
		 * count function
		 *
		 * the count function takes parameter for a query then returns the 
		 * total numver of matching models
		 *
		 * @param string $condition 
		 * @param string $order 
		 * @param string $limit 
		 * @param string $offset 
		 * @return integer|boolean
		 * @author Brian Bal
		 */
		
		function count($condition = "all", $order = "none", $limit = "none", $offset = "none")
		{
			$model = new $this->model_class();
			
			// init the found array
			$found = array();
			
			// build query parts
			$where = "";
			if($condition != "all") { $where = "WHERE $condition"; }
			
			$offset_limit = "";
			if($limit != "none") { $offset_limit = "LIMIT $limit"; }
			if($offset != "none") { $offset_limit = "$offset_limit OFFSET $offset"; }
			
			$order_by = "";
			if($order != "none") { $order_by = "ORDER BY $order"; }
			
			// build the query
			$sql = "SELECT
				count(1) as `count`
			FROM 
				`" . $model->getTableName() . "`
			$where
			$order_by
			$offset_limit
			";
			
			//echo "<pre>\n$sql\n</pre>";
			
			// run the query and get results
			$result = mysql_query($sql);
			if($row = mysql_fetch_array($result))
			{
				return $row['count'];
			}
			
			return false;
		}
		
		
		
		
		/**
		 * Join
		 *
		 * select the two tables with condition and other options
		 *
		 * @access	public
		 * @param	string $table
		 * @param	string $condition
		 * @param	string $order
		 * @param	string $limit
		 * @param	string $offset
		 * @param	string $type
		 * @return	array
		 */
		function join($table, $condition = "all", $order = "none", $limit = "none", $offset = "none")
		{
			$model = new $this->model_class();
			
			// init the found array
			$found = array();
			$table_columns = $model->getTableColumns();
			
			// build query parts
			$where = "";
			if($condition != "all") { $where = "WHERE $condition"; }
			
			$offset_limit = "";
			if($limit != "none") { $offset_limit = "LIMIT $limit"; }
			if($offset != "none") { $offset_limit = "$offset_limit OFFSET $offset"; }
			
			$order_by = "";
			if($order != "none") { $order_by = "ORDER BY $order"; }
			
			// build the query
			$sql = "SELECT
				*
			FROM 
				`" . $model->getTableName() . "`,
				`$table`
			$where
			$order_by
			$offset_limit
			";
			
			//echo "<pre>\n$sql\n</pre>";
			$class_name = $model->getClassName();
			$join_class_name = ucfirst(camelize(unplural($table)));
			$join_object_name = uncamelize($join_class_name);
			//echo "<h1>$join_class_name</h1>";
			
			$table_column_names = array();
			foreach($table_columns as $column)
			{
				$table_column_names[$column['Field']] = $column['Type'];
			}
			
			// run the query and get results
			$result = mysql_query($sql);
			while($row = mysql_fetch_array($result))
			{
				$tmp = new $class_name($table_columns);
				$tmp->$join_object_name = new $join_class_name();
				foreach($row as $key => $val)
				{
					if(isset($table_column_names[$key]))
					{
						$tmp->$key = $val;
					}else{
						$tmp->$join_object_name->$key = $val;
					}
				}
				array_push($found, $tmp);
				
			}
			
			return $found;
		}
		
		
		
		
		
		
	}
	
	
	
